package com.djk;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.List;

//搜索结果的适配器
public class SearchResultAdapter extends ArrayAdapter {
    private Context mContext;
    private List<Word> mWordList;

    public SearchResultAdapter(Context context, List<Word> wordList) {
        super(context, 0, wordList);
        mContext = context;
        mWordList = wordList;
    }

    //设置每一项的视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listItem = convertView;
        if (listItem == null) {
            //获取列表项的子视图
            listItem = LayoutInflater.from(mContext).inflate(R.layout.search_result_item, parent, false);
        }

        Word currentWord = mWordList.get(position);

        TextView textWord = listItem.findViewById(R.id.tv_result_word);
        textWord.setText(currentWord.getWord());

        TextView textMeaning = listItem.findViewById(R.id.tv_result_meaning);
        textMeaning.setText(currentWord.getMeaning());

        return listItem;
    }
}
